package com.wechat.ferry.strategy.msg.receive.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.google.common.collect.Lists;
import com.wechat.ferry.entity.dto.WxPpMsgDTO;
import com.wechat.ferry.entity.vo.request.WxPpWcfSendTextMsgReq;
import com.wechat.ferry.entity.vo.response.WxPpWcfGroupMemberResp;
import com.wechat.ferry.enums.WcfMsgTypeEnum;
import com.wechat.ferry.service.RobotService;
import com.wechat.ferry.service.WeChatDllService;
import com.wechat.ferry.service.WxPpWcfGroupMemberRespService;
import com.wechat.ferry.service.WxUserService;
import com.wechat.ferry.strategy.msg.receive.ReceiveMsgStrategy;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 策略实现类-接收消息-签到处理
 *
 * @author chandler
 * @date 2024-12-25 14:19
 */
@Slf4j
@Component
public class AiMsgStrategyImpl implements ReceiveMsgStrategy {

    @Autowired
    private WeChatDllService weChatDllService;
    @Autowired
    private WxUserService wxUserService;
    @Autowired
    private WxPpWcfGroupMemberRespService wxPpWcfGroupMemberRespService;
    @Autowired
    private RobotService robotService;

    @Override
    public String getStrategyType() {
        return WcfMsgTypeEnum.TEXT.getCode();
    }


    @Override
    public String doHandle(WxPpMsgDTO dto) {
        String sendMsg = getSendMsg(dto);
        if (ObjectUtil.isEmpty(sendMsg)) {
            return null;
        }
        //在发送消息
        WxPpWcfSendTextMsgReq wxPpWcfSendTextMsgReq = new WxPpWcfSendTextMsgReq();
        wxPpWcfSendTextMsgReq.setMsgText(sendMsg);
        if (Boolean.TRUE.equals(dto.getIsGroup())) {
            wxPpWcfSendTextMsgReq.setRecipient(dto.getRoomId());
        } else {
            wxPpWcfSendTextMsgReq.setRecipient(dto.getSender());
        }
        //获取查询用户
        WxPpWcfGroupMemberResp wcfGroupMemberResp = wxPpWcfGroupMemberRespService.getOne(Wrappers.<WxPpWcfGroupMemberResp>lambdaQuery().eq(WxPpWcfGroupMemberResp::getWeChatUid, dto.getSender()).last("limit 1"));
        if (ObjectUtil.isNotEmpty(wcfGroupMemberResp)) {
            wxPpWcfSendTextMsgReq.setAtUsers(Lists.newArrayList(dto.getSender()));
            wxPpWcfSendTextMsgReq.setMsgText("@" + wcfGroupMemberResp.getGroupNickName() + "\n" + wxPpWcfSendTextMsgReq.getMsgText());
        }
        weChatDllService.sendTextMsg(wxPpWcfSendTextMsgReq);
        return null;
    }


    @SneakyThrows
    private String imgMsg(WxPpMsgDTO dto) {
//        //下载文件
//        Thread.sleep(500);
//        weChatDllService.downloadImage(dto);
//        Thread.sleep(500);
//        //转换文件成图片
//        String targetFileName = GenericDatConverter.convert(dto.getExtra(), weChatFerryProperties.getImgPath());
//        Thread.sleep(200);
//        //解析二维码
//        List<String> codeUrlList = QRCodeParser.parseQRCodeFromImage(targetFileName);
//        if (CollUtil.isNotEmpty(codeUrlList)) {
//            return "解析到二维码个数:" + codeUrlList.size() + "\n" + codeUrlList.stream().collect(Collectors.joining("\n"));
//        }
//        return "没有解析到二维码";
        return null;
    }

    private String getAiMsg(WxPpMsgDTO dto) {
        String sendMsg;
        try {
            if (dto.getContent().contains("知乎热点新闻")) {
                sendMsg = robotService.getZhHotData();
            } else if (dto.getContent().contains("抖音热点新闻")) {
                sendMsg = robotService.getDyHotData();
            } else if (dto.getContent().contains("财联社热点新闻")) {
                sendMsg = robotService.getClsHotData();
            } else if (dto.getContent().contains("华尔街热点新闻")) {
                sendMsg = robotService.getHejHotData();
            } else {
                sendMsg = wxUserService.chatAdvancedAli(dto.getContent());
            }
        } catch (Exception e) {
            e.printStackTrace();
            sendMsg = "服务器出现了问题,请稍后重试";
        }
        if (ObjectUtil.isEmpty(sendMsg)) {
            sendMsg = "服务器出现了问题,请稍后重试";
        }
        return sendMsg;
    }

    private String getSendMsg(WxPpMsgDTO dto) {
        if (dto.getContent().contains("img") && ObjectUtil.isNotEmpty(dto.getExtra()) && Boolean.FALSE.equals(dto.getIsGroup())) {
            return imgMsg(dto);
        } else if ((dto.getContent().contains("@") && dto.getContent().contains("robot")) || Boolean.FALSE.equals(dto.getIsGroup())) {
            return getAiMsg(dto);
        }
        return null;
    }

}
